package com.sun.blog.sunblog.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import freemarker.ext.beans.BeansWrapper;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateHashModel;

public class FreemarkerUtil {
	protected static Logger log = Logger.getLogger(FreemarkerUtil.class);
	public static Configuration cfg;

	public FreemarkerUtil() throws IOException {
		init();
	}

	public static Map<String,Object> getInitMap(){
		Map<String,Object> map=new HashMap<String,Object>();
		try{
			/*	添加模板预定义参数和方法
			map.put("staticDomain", "");
			BeansWrapper wrapper = BeansWrapper.getDefaultInstance();
			TemplateHashModel enumModels = wrapper.getEnumModels();
			TemplateHashModel roundingModeEnums = (TemplateHashModel) enumModels
					.get("com.founder.pod.common.constants.ItemConstants");
	
			map.put("ItemConstants", roundingModeEnums);
			
			roundingModeEnums = (TemplateHashModel) enumModels
					.get("com.founder.pod.common.constants.TradeConstants");
			map.put("TradeConstants", roundingModeEnums);
			
			roundingModeEnums = (TemplateHashModel) enumModels
					.get("com.founder.pod.common.constants.UserConstants");
			map.put("UserConstants", roundingModeEnums);
			
			roundingModeEnums = (TemplateHashModel) enumModels
					.get("com.founder.pod.common.constants.CommonConstants");
			map.put("CommonConstants", roundingModeEnums);
			
			TemplateHashModel staticModels = wrapper.getStaticModels();
	
			TemplateHashModel ConstantsStatics = (TemplateHashModel) staticModels
					.get("com.founder.pod.common.constants.ItemConstants");
			map.put("ItemConstantsStatic", ConstantsStatics);
			
			ConstantsStatics = (TemplateHashModel) staticModels
					.get("com.founder.pod.common.constants.TradeConstants");
			map.put("TradeConstantsStatic", ConstantsStatics);
			
			ConstantsStatics = (TemplateHashModel) staticModels
					.get("com.founder.pod.common.constants.UserConstants");
			map.put("UserConstantsStatic", ConstantsStatics);
			
			ConstantsStatics = (TemplateHashModel) staticModels
					.get("com.founder.pod.common.constants.CommonConstants");
			map.put("CommonConstantsStatic", ConstantsStatics);*/
		}catch(Exception e){
			log.error("初始化模板默认参数错误", e);
		}
		return map;
	}
	/**
	 * 初始化方法 初始化Configuration
	 * 
	 * @param
	 * @exception 找不到文件
	 * @return 无返回值
	 */
	private static void init() throws IOException {
		cfg = new Configuration();
		cfg.setNumberFormat("#");
		cfg.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");
		cfg.setDateFormat("yyyy-MM-dd");
		cfg.setTimeFormat("HH:mm:ss");
		cfg.setDirectoryForTemplateLoading(
			new File(FreemarkerUtil.class.getClassLoader().getResource("/").getFile())
			);
		cfg.setDefaultEncoding("UTF-8");
	}

	/**
	 * 实例化 Configuration对象
	 * 
	 * @param
	 * @exception 找不到文件
	 * @return 无返Configuration 的一个实例化对象
	 */
	private static Configuration getConfiguration() throws IOException {
		if (cfg == null)
			init();
		return (cfg);
	}

	/**
	 * 通过模板生成页面
	 * 
	 * @param map
	 *            模板中需要的数据
	 * @param TemplatePath
	 *            模板路径
	 * @exception
	 * @return 返回生成的页面中的字符串
	 */
	
	public static String process(Map map, String templatePath) throws Exception {
		return process("UTF-8", map, templatePath);
	}

	/**
	 * 通过模板生成页面
	 * 
	 * @param map
	 *            模板中需要的数据
	 * @param TemplatePath
	 *            模板路径
	 * @exception
	 * @return 返回生成的页面中的字符串
	 */
	public static String process(String encoding, Map map, String templatePath)
			throws Exception {
		map.putAll(getInitMap());
		Configuration conf = getConfiguration(); //通过FreeMarker的Configuration对象可以读取ftl文件 
		Template temp = conf.getTemplate(templatePath, encoding);//在模板文件目录中相对应的flt文件
		StringWriter sw = new StringWriter();
		temp.process(map, sw);
		return (sw.toString());
	}

	public static void process(Map map, String templatePath, String savePath)
			throws Exception {
		map.putAll(getInitMap());
		Configuration conf = getConfiguration();
		Template temp = conf.getTemplate(templatePath);
		File file = new File(savePath);
		file.mkdirs();
		file.delete();
		OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
				file), "UTF-8");
		try {
			temp.process(map, osw);
		} catch (Exception e) {
			log.error("模版数据生成错误", e);
		}
	}
}
